Pain management algorithm